//
//  ARMovieViewController.m
//  AriseSamePro
//
//  Created by 包日守 on 15/11/2.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARMovieViewController.h"
#import "ARMovieCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ARMovieViewController ()<ARMovieCellDelegate>

@end

@implementation ARMovieViewController

- (void) viewWillAppear:(BOOL)animated
{
   //[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ARCate5Model *model = self.cateModels[indexPath.section];
    CGFloat w = [UIScreen mainScreen].bounds.size.width - 25;
    CGRect centextRect = [model.txt boundingRectWithSize:CGSizeMake(w, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
    
    //NSLog(@"%lf",centextRect.size.height);
    //NSLog(@"%@",model.txt);
    
    if ([model.txt isEqualToString:@""]) {
        return  336 - 21;
    }
    
    
    return centextRect.size.height + 336 - 21;
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ARMovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ARMovieCell"];
    
    if (cell == nil) {
        
        
        [tableView registerNib:[UINib nibWithNibName:@"ARMovieCell" bundle:nil] forCellReuseIdentifier:@"ARMovieCell"];
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"ARMovieCell"];
        
    }
    cell.cateModel = self.cateModels[indexPath.section];
    cell.delegate = self;
    
    [cell setShareBtnBlock:^(ARCate5Model *cateModel, UIButton *sender) {
        [self createShare:cateModel WithSender:sender];
        
    }];
    
    
    return cell;
    
    
}

#pragma mark - ARMoiveCellDelegate
- (void) arMovieCell:(ARMovieCell *)cell WithCate:(ARCate5Model *)cateModel
{
   // MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:cateModel.mediaModel.movieModel.trailer]];
    //[self presentViewController:player animated:YES completion:nil];
    
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init];
    playerVC.player = [AVPlayer playerWithURL:[NSURL URLWithString:cateModel.mediaModel.movieModel.trailer]];
   // NSLog(@"%@",cateModel.mediaModel.movieModel.trailer);
    [playerVC.player play];
    
    
    playerVC.view.transform =CGAffineTransformMakeRotation((M_PI / 2.0));
   
    
    //[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
    [self presentViewController:playerVC animated:YES completion:nil];
    
    /*
    NSURL *url = [NSURL URLWithString:cateModel.mediaModel.movieModel.trailer];
    
   // NSString *filePath = [[NSBundle mainBundle] pathForResource:@"backspace" ofType:@"mov"];
   // NSURL *sourceMovieURL = [NSURL fileURLWithPath:filePath];
    
    AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = self.view.layer.bounds;
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    
    [self.view.layer addSublayer:playerLayer];
    [player play];
*/
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
